#include "BlizzardAPI.j"

library Hardware initializer Init requires BzAPI

    /*
        鼠标滚轮控制视距
        一键切换宽屏模式
        made by 裂魂
        2018/10/19
    */

    globals
        //初始视野等级
        integer ViewLevel = 18
        //开启重置镜头属性标识
        private boolean ResetCam = true
        //镜头变化平滑度
        private real WheelSpeed = 0.1
        //是否是宽屏
        private boolean WideScr = false
        //默认X轴角度
        private real X_ANGLE = 304
    endglobals

    //鼠标滚轮变化时调用
    private function OnWheel takes nothing returns nothing
        //滚轮变化量
        local integer delta = DzGetWheelDelta()
        //如果鼠标不在游戏内，就不响应鼠标滚轮  
        if not DzIsMouseOverUI() or DzGetMouseFocus() != 0 then
            return
        endif
        //标记需要重置镜头属性
        set ResetCam = true
        if delta < 0 then
            //滚轮下滑
            if ViewLevel < 20 then  //视野等级上限
                set ViewLevel = ViewLevel + 1
            endif
        else
            //滚轮上滑
            if ViewLevel > 8 then   //视野等级下限
                set ViewLevel = ViewLevel - 1
            endif
        endif
        //记录滚动前的镜头角度
        set X_ANGLE = Rad2Deg(GetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK))
    endfunction

    //每帧渲染时调用
    function JinTouUpdate takes nothing returns nothing
        if ResetCam then
            //重设镜头角度和高度
            call SetCameraField( CAMERA_FIELD_ANGLE_OF_ATTACK, X_ANGLE, 0 )
            call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, ViewLevel*200, WheelSpeed)
            set ResetCam = false
        endif
    endfunction

    //按键触发
    function SetWideScreen takes nothing returns nothing
        set WideScr = not WideScr
        call DzEnableWideScreen(WideScr)
    endfunction

    private function Init takes nothing returns nothing
        //注册滚轮事件
        call DzTriggerRegisterMouseWheelEventByCode( null, false, function OnWheel)
        //注册每帧渲染事件
        // call DzFrameSetUpdateCallbackByCode(function JinTouUpdate)
        //call Hard.updateCallEvent(function Update)
        //注册按下键码为145的按键(ScrollLock)事件
        // call DzTriggerRegisterKeyEventByCode( null, 145, 1, false, function SetWideScreen)
    endfunction

endlibrary

